﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Tilver.Net;
using Tilver.Data;
using System.Data.Common;

namespace Tilver.WinTest
{
    public partial class UIForm : Form
    {
        AsyncTaskUI _Task;

        public UIForm()
        {
            InitializeComponent();

            //创建任务管理对象
            _Task = new AsyncTaskUI();
            //挂接事件处理方法
            _Task.TaskStatusChanged += new TaskEventHandler(OnTaskStatusChanged);
            _Task.TaskProgressChanged += new TaskEventHandler(OnTaskProgressChanged1);
            _Task.TaskProgressChanged += new TaskEventHandler(OnTaskProgressChanged2);
        }

        #region 私有方法
        //在UI线程,负责更新进度条
        private void OnTaskProgressChanged1(object sender, TaskEventArgs e)
        {
            if (this.InvokeRequired)		//不在UI线程上,异步调用
            {
                TaskEventHandler TPChanged1 = new TaskEventHandler(OnTaskProgressChanged1);
                this.BeginInvoke(TPChanged1, new object[] { sender, e });
                Console.WriteLine("InvokeRequired=true");
            }
            else
            {
                progressBar.Value = e.Progress;
            }
        }
        //在UI线程,负责更新完成率
        private void OnTaskProgressChanged2(object sender, TaskEventArgs e)
        {
            label_progress.Text = string.Format("进度[{0}%],中间结果:{1}.", e.Progress.ToString(), e.Result != null ? e.Result.ToString() : "null");
        }
        //在UI线程,负责更新状态信息和按钮状态
        private void OnTaskStatusChanged(object sender, TaskEventArgs e)
        {
            string msg = string.Empty;
            switch (e.Status)
            {
                case TaskStatus.Running:
                    button_start.Enabled = false;
                    button_abort.Enabled = true;
                    button_fireerror.Enabled = true;
                    button_stop.Enabled = true;
                    break;
                case TaskStatus.CancelPending:
                    button_start.Enabled = false;
                    button_abort.Enabled = false;
                    button_fireerror.Enabled = false;
                    button_stop.Enabled = false;
                    break;
                case TaskStatus.AbortPending:
                    button_start.Enabled = false;
                    button_abort.Enabled = false;
                    button_fireerror.Enabled = false;
                    button_stop.Enabled = false;
                    break;
                case TaskStatus.Stopped:
                    button_start.Enabled = true;
                    button_abort.Enabled = false;
                    button_fireerror.Enabled = false;
                    button_stop.Enabled = false;
                    label_progress.Text = string.Empty;
                    progressBar.Value = 0;
                    break;
                case TaskStatus.ThrowErrorStoped:
                    button_start.Enabled = true;
                    button_abort.Enabled = false;
                    button_fireerror.Enabled = false;
                    button_stop.Enabled = false;
                    label_progress.Text = string.Empty;
                    progressBar.Value = 0;
                    msg = _Task.Exception != null ? _Task.Exception.Message : "";
                    break;
                case TaskStatus.Aborted:
                    button_start.Enabled = true;
                    button_abort.Enabled = false;
                    button_fireerror.Enabled = false;
                    button_stop.Enabled = false;
                    label_progress.Text = string.Empty;
                    progressBar.Value = 0;
                    msg = "被调用者强行中止.";
                    break;
            }
            if (_Task.WorkThread != null)
                label_status.Text = string.Format("任务状态:{0},线程名称:{1},线程编号:{2},线程状态:{3},运行结果:{4},其它信息:{5}.", e.Status, _Task.WorkThread.Name, _Task.WorkThread.GetHashCode(), _Task.WorkThread.ThreadState, e.Result != null ? e.Result.ToString() : "null", msg);
            else
                label_status.Text = string.Format("任务状态:{0},运行结果:{1},工作线程属性为NULL,其它信息:{2}.", e.Status, e.Result != null ? e.Result.ToString() : "null", msg);

        }

        #endregion

        private void button_start_Click(object sender, EventArgs e)
        {
            _Task.errorkey = 1;
            _Task.StartTask(new object[] { });


        }

        private void button_abort_Click(object sender, EventArgs e)
        {
            _Task.AbortTask();
        }

        private void button_fireerror_Click(object sender, EventArgs e)
        {
            _Task.errorkey = 0;
        }

        private void button_stop_Click(object sender, EventArgs e)
        {
            _Task.StopTask();
        }

        private void label_status_Click(object sender, EventArgs e)
        {
            MessageBox.Show(_Task.TaskState.ToString() + ",CallThread:[" + _Task.CallThread.GetHashCode().ToString() + "],WorkThread:[" + _Task.WorkThread.GetHashCode().ToString() + "]");
        }
        #region 数据库操作
        //查询
        private void button_QueryData_Click(object sender, EventArgs e)
        {
            ShowGV();
        }

        private void ShowGV()
        {
            var bo = new DataTest();
            //var list = bo.DbDataBase_Get();
            var list = bo.DbAdoBase_Get();
            dataGridView1.DataSource = list;
        }
        //添加
        private void button_add_Click(object sender, EventArgs e)
        {
            var bo = new DataTest();
            bo.DbAdoBase_Add(new CustomerInfoEO()
            {
                Address = textBox_address.Text,
                LogDate = dateTimePicker_logdate.Value,
                Phone = textBox_phone.Text,
                UserName = textBox_username.Text
            });
            /*
            bo.DbDataBase_Add(new CustomerInfoEO()
            {
                Address = textBox_address.Text,
                LogDate = dateTimePicker_logdate.Value,
                Phone = textBox_phone.Text,
                UserName = textBox_username.Text
            });
             */
            ShowGV();
        }
        //删除
        private void button_del_Click(object sender, EventArgs e)
        {
            var bo = new DataTest();
            bo.DbAdoBase_Del(int.Parse(textBox_ID.Text));
            //bo.DbDataBase_Del(int.Parse(textBox_ID.Text));
            ShowGV();
        }
        //修改
        private void button_update_Click(object sender, EventArgs e)
        {
            var bo = new DataTest();
            bo.DbAdoBase_Update(int.Parse(textBox_ID.Text), new CustomerInfoEO()
            {
                Address = textBox_address.Text,
                LogDate = dateTimePicker_logdate.Value,
                Phone = textBox_phone.Text,
                UserName = textBox_username.Text
            });
            /*
            bo.DbDataBase_Update(int.Parse(textBox_ID.Text), new CustomerInfoEO()
            {
                Address = textBox_address.Text,
                LogDate = dateTimePicker_logdate.Value,
                Phone = textBox_phone.Text,
                UserName = textBox_username.Text
            });
             * */
            ShowGV();
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var rowIndex = e.RowIndex;
            var rows = dataGridView1.Rows[rowIndex];

            textBox_ID.Text = rows.Cells[0].Value.ToString();
            textBox_username.Text = rows.Cells[1].Value.ToString();
            textBox_address.Text = rows.Cells[2].Value.ToString();
            textBox_phone.Text = rows.Cells[3].Value.ToString();
            dateTimePicker_logdate.Value = string.IsNullOrEmpty(rows.Cells[4].Value.ToString()) ? DateTime.Now : DateTime.Parse(rows.Cells[4].Value.ToString());
        }
        #endregion



    }
}
